package com.xmkj.winmoney.home;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.Entity.HomeHeadEntity;
import com.common.Entity.ShareEntity;
import com.common.base.Config;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.GetShareDatasMethods;
import com.common.retrofit.method.ShareSuccessMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.xmkj.payandlogin.ShareConfig;
import com.xmkj.payandlogin.ShareManager;
import com.xmkj.payandlogin.ShareUtil;
import com.xmkj.payandlogin.share.ShareListener;
import com.xmkj.payandlogin.share.SharePlatform;
import com.xmkj.winmoney.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/5/25
 * 描述：
 */

public class ZhongJiangTipActivity extends BaseMvpActivity {
    public static final String INFO = "info";
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_qi)
    TextView tvQi;
    @BindView(R.id.tv_share)
    TextView tv_share;

    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.fl_ll)
    FrameLayout flLl;
    private HomeHeadEntity homeHeadEntity;
    private ShareEntity shareEntity;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zhongjiang_tip;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        ShareConfig config = ShareConfig.instance()
                .qqId(Config.APP_QQ_ID)
                .wxSecret(Config.APP_SERECET)
                .wxId(Config.APP_ID);
        ShareManager.init(config);
        homeHeadEntity = (HomeHeadEntity) getIntent().getSerializableExtra(INFO);
        if(EmptyUtils.isNotEmpty(homeHeadEntity)){
            tvQi.setText("[期号："+homeHeadEntity.luck_info.fvid+"]");
            tvInfo.setText(homeHeadEntity.luck_info.goods_name);
        }
        getShareData();
        attachClickListener(tv_share);
        attachClickListener(ivClose);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==tv_share.getId()){
            gotoShare();
        }else if(view.getId()==ivClose.getId()){
            onBackPressed();
        }
    }
    private void gotoShare() {
        showShareButton();
        setOnShareClick(new OnShareClick() {
            @Override
            public void wechat() {
                if (ShareUtil.isInstalled(SharePlatform.WX, context)) {

//                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
//                    if (EmptyUtils.isEmpty(mineBean.img)) {
//                        ShareUtil.shareMedia(context, SharePlatform.WX, mineBean.title, mineBean.content, mineBean.url, bitmap, shareListener);
//
//                    } else {
//                        ShareUtil.shareMedia(context, SharePlatform.WX, mineBean.title, mineBean.content, mineBean.url, mineBean.img, shareListener);
                    ShareUtil.shareMedia(context, SharePlatform.WX, shareEntity.share_title.equals("") ? "拼夺宝" : shareEntity.share_title, shareEntity.share_content, shareEntity.share_url, shareEntity.logo, shareListener);
//                    }

                } else {
                    showToastMsg("未安装微信");
                }
            }

            @Override
            public void wethatCircle() {
                if (ShareUtil.isInstalled(SharePlatform.WX_TIMELINE, context)) {
//                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
//                    if (EmptyUtils.isEmpty(mineBean.img)) {
//                        ShareUtil.shareMedia(context, SharePlatform.WX_TIMELINE, mineBean.title, mineBean.content, mineBean.url, bitmap, shareListener);
//
//                    } else {
//                        ShareUtil.shareMedia(context, SharePlatform.WX_TIMELINE, mineBean.title, mineBean.content, mineBean.url, mineBean.img, shareListener);

                    ShareUtil.shareMedia(context, SharePlatform.WX_TIMELINE, shareEntity.share_title.equals("") ? "拼夺宝" : shareEntity.share_title, shareEntity.share_content, shareEntity.share_url, shareEntity.logo, shareListener);
//                    }
                } else {
                    showToastMsg("未安装微信");
                }
            }

            @Override
            public void qq() {
                if (ShareUtil.isInstalled(SharePlatform.QQ, context)) {
//                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
               /*     if (EmptyUtils.isEmpty(mineBean.img)) {
                        ShareUtil.shareMedia(context, SharePlatform.QQ, mineBean.title, mineBean.content, mineBean.url, bitmap, shareListener);

                    } else {*/
//                        ShareUtil.shareMedia(context, SharePlatform.QQ, mineBean.title, mineBean.content, mineBean.url, mineBean.img, shareListener);
                    ShareUtil.shareMedia(context, SharePlatform.QQ, shareEntity.share_title.equals("") ? "拼夺宝" : shareEntity.share_title, shareEntity.share_content, shareEntity.share_url, shareEntity.logo, shareListener);

//                    }

                } else {
                    showToastMsg("未安装QQ");
                }
            }
        });
    }

    private void Sharesuccessfully() {
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        ShareSuccessMethods.getInstance().shareSuccess(subscriber, uid, hashid);
        rxManager.add(subscriber);
    }
    private ShareListener shareListener = new ShareListener() {
        @Override
        public void shareSuccess() {
            showToastMsg("分享成功");
            Sharesuccessfully();
        }

        @Override
        public void shareFailure(Exception e) {
            showToastMsg("分享失败");
        }

        @Override
        public void shareCancel() {
            showToastMsg("取消分享");
        }
    };
    @Override
    protected void setNavigation() {
        super.setNavigation();
        m_statusBar.setVisibility(View.GONE);
    }



    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }

    private void getShareData() {
        CommonSubscriber<ShareEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                shareEntity = (ShareEntity) o;

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        GetShareDatasMethods.getInstance().share(subscriber, uid, hashid);
        rxManager.add(subscriber);
    }
}
